package 线程池;

import jdk.nashorn.internal.codegen.CompilerConstants;

import java.util.concurrent.*;

public class test extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(this.getName() + "---->" + i);
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建线程池，可以最大放入6个线程
        ExecutorService service = Executors.newFixedThreadPool(6);
        //使用execute方法执行Thread的类型的线程
        service.execute(new test());
        service.execute(new test());
        //使用submit方法执行Callable的类型的线程
        Future<Boolean> a = service.submit(new callable());
        System.out.println(a.get());
        Future<Boolean> a1 = service.submit(new callable());
        System.out.println(a1.get());
        //使用execute方法执行Runnable的类型的线程
        service.execute(new Thread(new runnable(), "runnable1"));
        service.execute(new Thread(new runnable(), "runnable2"));



        //关闭线程池
        service.shutdown();

    }


}

class callable implements Callable<Boolean> {

    @Override
    public Boolean call() throws Exception {
        for (int i = 1; i < 20; i++) {
            if (i%19 == 19) {
                return true;
            }

            System.out.println(Thread.currentThread().getName() + "我是--->Callable" + i);
        }
        return false;
    }
}

class runnable implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "我是--->Runnable" + i);
        }
    }
}